package de.haw;

import jgame.JGColor;
import jgame.JGImage;
import jgame.JGPoint;
import jgame.platform.JGEngine;
import de.haw.vehicle.IVehicle;
import de.haw.vehicle.Porsche911GT2RS;
import de.haw.world.Earth;
import de.haw.world.IWorld;

public class View extends JGEngine {

	private static final int WINDOW_WIDTH = 1024;
	private static final int WINDOW_HEIGHT = 768;
	private static final String porscheBildName = "PorscheGrafik";
	private static final String nasserAsphaltBildName = "NasserAsphaltGrafik";
	private static final String asphaltBildName = "AsphaltGrafik";
	private static final String eisBildName = "EisGrafik";
	private static final String schneeBildName = "SchneeGrafik";
	private static Controller controller;
	private static IVehicle vehicle;
	private static IWorld earth;
	private JGPoint imageSize;

	public static void main(String [] args) {
		earth = new Earth();
		vehicle = new Porsche911GT2RS(earth);
		vehicle.setASR(true);
		controller = new Controller(vehicle, earth);
		// We start the engine with a fixed window size (which happens to
		// be twice the size of the defined playfield, scaling the playfield
		// by a factor 2).  Normally, you'd want this size to be configurable,
		// for example by means of command line parameters.
		new View(new JGPoint(WINDOW_WIDTH, WINDOW_HEIGHT));
	}


	/** The parameterless constructor is called by the browser, in case we're
	 * an applet. */
	public View() {
		// This inits the engine as an applet.
		initEngineApplet(); 
	}

	/** We use a separate constructor for starting as an application. */
	public View(JGPoint size) {
		// This inits the engine as an application.
		initEngine(size.x,size.y); 
	}

	/** This method is called by the engine when it is ready to intialise the
	 * canvas (for an applet, this is after the browser has called init()).
	 * Note that applet parameters become available here and not
	 * earlier (don't try to access them from within the parameterless
	 * constructor!).  Use isApplet() to check if we started as an applet.
	 */
	public void initCanvas() {
		// The only thing we need to do in this method is to tell the engine
		// what canvas settings to use.  We should not yet call any of the
		// other game engine methods here!
		setCanvasSettings(
			20,  // width of the canvas in tiles
			15,  // height of the canvas in tiles
			16,  // width of one tile
			16,  // height of one tile
			     //    (note: total size = 20*16=320  x  15*16=240)
			null,// foreground colour -> use default colour white
			null,// background colour -> use default colour black
			null // standard font -> use default font
		);
	}

	/** This method is called when the engine has finished initialising and is
	 * ready to produce frames.  Note that the engine logic runs in its own
	 * thread, separate from the AWT event thread, which is used for painting
	 * only.  This method is the first to be called from within its thread.
	 * During this method, the game window shows the intro screen. */
	public void initGame() {
		// We can set the frame rate, load graphics, etc, at this point. 
		// (note that we can also do any of these later on if we wish)
		setFrameRate(
			50,// fps
			2  //  2 = frame skip, skip at most 2 frames before displaying
			   //      a frame again
		);
		defineImage(porscheBildName, "Car", 0, "Porsche.png", "-");
	    imageSize = getImageSize(porscheBildName);
	    
	    defineImage(asphaltBildName, "Asphalt", 0, "Asphalt.jpg", "-");
	    defineImage(eisBildName, "Eis", 0, "Ice.jpg", "-");
	    defineImage(schneeBildName, "Schnee", 0, "Schnee.jpg", "-");	
	    defineImage(nasserAsphaltBildName, "Nasser Asphalt", 0, "Nass_Asphalt.jpg", "-");
	
	}

	/** A timer used to animate the "hello world" text. */
	double texttimer=0;
	private boolean accelerate;
	private double deltaTime = 0.01;
	private boolean brake;
	private double gasLevel;
	private double brakeLevel;
	private int x = 0;
	/** Game logic is done here.  No painting can be done here, define
	* paintFrame to do that. */
	public void doFrame() {
		accelerate = false;
		brake = false;
		// Increment the angle of the moving text.
		texttimer += 0.05;
		
		if (vehicle.getSpeedInKmh() > 1) {
			x += 16;
			if (x >= WINDOW_WIDTH) x = -imageSize.x;
		}
		
		if(getKey(KeyUp)) {
			accelerate = true;
		}
		
		if(getKey(KeyDown)) {
			brake = true;
		}
		
		if(getKey(KeyCtrl)) {
			clearKey(KeyCtrl);
			vehicle.setABS(!vehicle.isABS());
		}
		
		if(getKey(KeyShift)) {
			clearKey(KeyShift);
			vehicle.setASR(!vehicle.isASR());
		}
		
	
		//Untergrundunterschiede:
		//Taste 49 ist die 1
		if(getKey(49)){
			clearKey(49);System.out.println("View.doFrame()");
			earth.setGround(IWorld.GROUND_STREET);
		}
		//Taste 50 ist die 2
		if(getKey(50)){
			clearKey(50);
			earth.setGround(IWorld.GROUND_WET_STREET);
		}
		//Taste 51 ist die 3
		if(getKey(51)){
			clearKey(51);
			earth.setGround(IWorld.GROUND_SNOW);
		}
		//Taste 52 ist die 4
		if(getKey(52)){
			clearKey(52);
			earth.setGround(IWorld.GROUND_ICE);
		}
	}

	/** Any graphics drawing beside objects or tiles should be done here.
	 * Usually, only status / HUD information needs to be drawn here. */
	public void paintFrame() {
		if (IWorld.GROUND_STREET == earth.getGround()){
			drawImage(0, 0, asphaltBildName);
		}else if (IWorld.GROUND_WET_STREET == earth.getGround()){
			drawImage(0, 0, nasserAsphaltBildName);
		}else if (IWorld.GROUND_SNOW == earth.getGround()){
			drawImage(0, 0, schneeBildName);
		}else if (IWorld.GROUND_ICE == earth.getGround()){
			drawImage(0, 0, eisBildName);
		}
		setColor(JGColor.red);
		if (accelerate) {
			if (gasLevel < 1.0){
				gasLevel += 0.01;
			}
			vehicle.setLevel(gasLevel);
			controller.accelerate(deltaTime);
			drawString("currentSpeed: " + (int)vehicle.getSpeedInKmh(), 50, 10, 0);
		} else {
			gasLevel = 0.0;
			vehicle.setLevel(gasLevel);
			controller.accelerate(deltaTime);
			drawString("currentSpeed: " + (int)vehicle.getSpeedInKmh(), 50, 10, 0);
		}
		
		if (brake) {
			if (brakeLevel < 1.0){
				brakeLevel = 1.0;
				brakeLevel += 0.01;
			} else if (brakeLevel <= 1.5) {
				brakeLevel += 0.01;
			}
			vehicle.setBrakelevel(brakeLevel);
			controller.brake(deltaTime);
			drawString("Bremse getreten", 50, 100, 0);
		} else {
			brakeLevel = 0.0;
			vehicle.setBrakelevel(brakeLevel);
			controller.brake(deltaTime);
		}
		drawString("ABS: " + vehicle.isABS(), 300, 50, 0);
		drawString("ASR: " + vehicle.isASR(), 300, 80, 0);
		drawString("Traktion " + earth.getGround(), 150,100,0);
		drawImage(x, 150, "PorscheGrafik");

		drawLine(0, 120, WINDOW_WIDTH, 120, 2.0, new JGColor(255, 255, 0));
	}
}